package com.yw.atp.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;

import com.yw.atp.model.Command;
import com.yw.atp.model.Consult;
import com.yw.atp.model.Reply;
import com.yw.atp.model.ReplyBean;
import com.yw.atp.model.Weather;

public class BeanUtil {
	private static final String PREX = ";;";
    public static String formatCommand(Command command,Reply r){
    	String isRead = "";
    	String rc = "";
    	String rt = " ";
    	if(r!=null){
    		isRead = Constants.IMPORT_NOTICE_READED;
    		rc = r.getContent();
    		rt = DateUtil.getClientFormatA(r.getCreatedAt());
    	}else{
    		isRead = Constants.IMPORT_NOTICE_UN_READ;
    	}
    	if(command!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(command.getId());
			sb.append(PREX+getValue(command.getCommType()));
			sb.append(PREX+"{"+getValue(command.getContent()+"}"));
			sb.append(PREX+"{"+(getValue(command.getAtpImage()==null?"":command.getAtpImage().getPath())+"}"));
			sb.append(PREX+getValue(command.getIssuer()==null?"":command.getIssuer().getId()));
			sb.append(PREX+getValue(command.getIssueTime()==null?"":DateUtil.getClientFormatA(command.getIssueTime())));
			sb.append(PREX+getValue(command.getSender()==null?"":command.getSender().getId()));
			sb.append(PREX+getValue(command.getSendTime()==null?"":DateUtil.getClientFormatA(command.getSendTime())));
			sb.append(PREX+getValue(command.getSendType()));
			sb.append(PREX+getValue(command.isTop()?1:0));
			sb.append(PREX+getValue(isRead));
			sb.append(PREX+getValue(rc));
			sb.append(PREX+getValue(rt));
			return sb.toString();
		}else{
			return null;
		}
    }
    
    public static String formatReply(Reply reply){
    	if(reply!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(reply.getOwner().getId());
			sb.append(PREX+getValue(reply.getContent()));
			sb.append(PREX+getValue(reply.getCreatedAt()==null?" ":DateUtil.getClientFormatA(reply.getCreatedAt())));
			return sb.toString();
		}else{
			return null;
		}
    }
    
    public static String formatWeather(Weather we){
		if(we!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(we.getId());
			sb.append(PREX+getValue(we.getDate()==null?"":DateUtil.getClientFormatB(we.getDate())));
			sb.append(PREX+getValue(we.getMessage()));
			sb.append(PREX+getValue(we.getStatusPeriod1()));
			sb.append(PREX+getValue(we.getTypePeriod1()));
			sb.append(PREX+getValue(we.getStatusPeriod2()));
			sb.append(PREX+getValue(we.getTypePeriod2()));
			sb.append(PREX+getValue(we.getStatusPeriod3()));
			sb.append(PREX+getValue(we.getTypePeriod3()));
			sb.append(PREX+getValue(we.getStatusPeriod4()));
			sb.append(PREX+getValue(we.getTypePeriod4()));
			sb.append(PREX+getValue(we.getStatusPeriod5()));
			sb.append(PREX+getValue(we.getTypePeriod5()));
			sb.append(PREX+getValue(we.getStatusPeriod6()));
			sb.append(PREX+getValue(we.getTypePeriod6()));
			sb.append(PREX+getValue(we.getStatusPeriod7()));
			sb.append(PREX+getValue(we.getTypePeriod7()));
			sb.append(PREX+getValue(we.getStatusPeriod8()));
			sb.append(PREX+getValue(we.getTypePeriod8()));
			sb.append(PREX+getValue(we.getStatusPeriod9()));
			sb.append(PREX+getValue(we.getTypePeriod9()));
			sb.append(PREX+getValue(we.getStatusPeriod10()));
			sb.append(PREX+getValue(we.getTypePeriod10()));
			sb.append(PREX+getValue(we.getStatusPeriod11()));
			sb.append(PREX+getValue(we.getTypePeriod11()));
			sb.append(PREX+getValue(we.getStatusPeriod12()));
			sb.append(PREX+getValue(we.getTypePeriod12()));
			sb.append(PREX+getValue(we.getStatusHalf1()));
			sb.append(PREX+getValue(we.getTypeHalf1()));
			sb.append(PREX+getValue(we.getStatusHalf2()));
			sb.append(PREX+getValue(we.getTypeHalf2()));
			sb.append(PREX+getValue(we.getStatusHalf3()));
			sb.append(PREX+getValue(we.getTypeHalf3()));
			sb.append(PREX+getValue(we.getStatusHalf4()));
			sb.append(PREX+getValue(we.getTypeHalf4()));
			sb.append(PREX+getValue(we.getUpdateTime()));
			sb.append(PREX+getValue(we.getVersion()));
			sb.append(PREX+getValue(we.getTem()));
			sb.append(PREX+(we.getCurrentTypePeriod()!=null?we.getCurrentTypePeriod():" "));
			return sb.toString();
		}else{
			return null;
		}
	}
    
    public static String formatConsult(Consult co){
		if(co!=null){
			StringBuffer sb = new StringBuffer();
			sb.append(co.getId());
			sb.append(PREX+getValue(co.getContent()));
			sb.append(PREX+getValue(co.getCreatedAt()==null?" ":DateUtil.getClientFormatA(co.getCreatedAt())));
			return sb.toString();
		}else{
			return null;
		}
	}
        
    public static List<ReplyBean> parReply(String str){
    	List<ReplyBean> rl = new ArrayList<ReplyBean>();
    	try{
    		JSONArray jsa = new JSONArray(str);
    		for(int i=0;i<jsa.length();i++){
				String s = (String)jsa.get(i);
				System.out.println(s);
				String[] ss = s.split(PREX);
				ReplyBean rb = new ReplyBean();
				if(ss.length==3){
					rb.servID = ss[0];
					rb.content = ss[1];
					rb.createdAt = ss[2];
				}else if(ss.length==2){
					rb.servID = ss[0];
					rb.content = "已读";
					rb.createdAt = DateUtil.getClientFormatA(new Date());
				}
				rl.add(rb);
			}
    		return rl;
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	return null;
    }
    
	public static String getValue(Object v){
		if(v==null){
			return "";
		}else{
			return String.valueOf(v);
		}
	}

}
